package a4;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;

public class CorpusProcessor {

	public static void run(HashMap<String, Integer> tCount, HashMap<String, Integer> tFirstCount, HashMap<String, Integer> ttCount, HashMap<String, HashMap<String, Integer>> wtCount) {

		try {
			File file = new File("CoNLL2009-ST-English-train-pos.txt");
			LineIterator it = FileUtils.lineIterator(file, "UTF-8");

			while (it.hasNext()) {
				String line = it.nextLine();
				if (line.isEmpty()) continue;
				String[] cols = line.split("\t");
				if (cols[1].equals(".")) continue;

				// POS tag count
				if (!tCount.containsKey(cols[4])) {
					tCount.put(cols[4], 1);
				} else {
					tCount.put(cols[4], tCount.get(cols[4]) + 1);
				}

				// Word-POS pairs + count
				if (!wtCount.containsKey(cols[2])) {
					HashMap<String, Integer> h = new HashMap<String, Integer>();
					h.put(cols[4], 1);
					wtCount.put(cols[2], h);
				} else {
					if (wtCount.get(cols[2]).containsKey(cols[4])) {
						wtCount.get(cols[2]).put(cols[4],
								wtCount.get(cols[2]).get(cols[4]) + 1);

					} else {
						wtCount.get(cols[2]).put(cols[4], 1);
					}
				}

				// First word in sentence
				if (cols[0].equals("1")) {
					if (!tFirstCount.containsKey(cols[4])) {
						tFirstCount.put(cols[4], 1);
					} else {
						tFirstCount.put(cols[4], tFirstCount.get(cols[4]) + 1);
					}
				}

				if (it.hasNext()) {
					String secondLine = it.nextLine();
					String[] secondCols;
					// Process second line if it contains word+POS
					if (secondLine.isEmpty()) continue;
					secondCols = secondLine.split("\t");;
					if (secondCols[1].equals(".")) continue;

					// POS tag count
					if (!tCount.containsKey(secondCols[4])) {
						tCount.put(secondCols[4], 1);
					} else {
						tCount.put(secondCols[4], tCount.get(secondCols[4]) + 1);
					}

					// Word-POS pairs count
					if (!wtCount.containsKey(secondCols[2])) {
						HashMap<String, Integer> h = new HashMap<String, Integer>();
						h.put(secondCols[4], 1);
					} else {
						if (wtCount.get(secondCols[2]).containsKey(
								secondCols[4])) {
							wtCount.get(secondCols[2]).put(
									secondCols[4],
									wtCount.get(secondCols[2]).get(
											secondCols[4]) + 1);
						} else {
							wtCount.get(secondCols[2]).put(secondCols[4], 1);
						}
					}

					// POS bigram pairs count
					if (!ttCount.containsKey(cols[4] + secondCols[4])) {
						ttCount.put(cols[4] + secondCols[4], 1);
					} else {
						ttCount.put(cols[4] + secondCols[4],
								ttCount.get(cols[4] + secondCols[4]) + 1);
					}

				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
